use crate::component::request::Request;
use crate::component::response::Response;
use async_trait::async_trait;
use reqwest;

/// 下载中间件处理 request 和 response 时的返回结果
/// 用于指明后续处理的方式
pub enum DownloaderMwResult {
    /// 停止处理该请求, 不再继续调用 `process_request()`, 重新调度该 request.
    FinalRequest(Request),
    /// 继续处理该请求, 直到最后交给下载器发送该 request
    IntermediateRequest(Request),
    /// 不再调用任何的 `process_request()`, `process_response()`, 或者下载;
    /// 而是直接返回该 response
    Response(Response),
    Ignore,
}

/// 指定当下载发生下载错误时如何处理
pub enum DownloaderMwErrResult {
    /// 继续调用 `process_error()` 处理
    Continue,
    Request(Request),
    Response(Response),
}

#[async_trait]
pub trait DownloaderMiddleware: Send {
    async fn process_request(&mut self, request: Request) -> DownloaderMwResult;
    async fn process_response(
        &mut self,
        request: &Request,
        response: Response,
    ) -> DownloaderMwResult;
    async fn process_error(
        &mut self,
        request: &Request,
        error: &reqwest::Error,
    ) -> DownloaderMwErrResult;
}
